<template>
	<div>
    <div class="container">
		<div class="handle-box">
            <label class="label" >专业方向：</label>
            <el-input v-model="courseName" placeholder="搜索" class="handle-input mr10" @blur="handleSearch"></el-input>
        	<el-button color="#0ab394"  @click="handleSearch">查询</el-button>
		
            <el-row class="top-btn">
     			<!-- <el-button-group class="ml-4"> -->
                    <el-button color="#0ab394"  @click="open">添加</el-button> 
                    <el-button color="#e35148" @click="handleAllDelete">删除</el-button>                         
       		 <!-- </el-button-group> -->
			</el-row>
        </div>
			<el-table 
            ref="multipleTableRef" 
				:header-cell-style="{color:'#2A62BC'}"
			 	:data="tableData"   
			 	style="width: 100%;" 
                size="small"
                border
				@selection-change="handleSelectionChange">
                <el-table-column   type="selection" width="35" />
				<el-table-column  prop="id" label="序号"  align="center" width="45"></el-table-column>
				<el-table-column prop="courseNumber" label="课程编号" align="center" ></el-table-column>
				<el-table-column prop="courseName" label="课程名称" align="center"></el-table-column>
				<el-table-column prop="courseDesc" label="课程说明" align="center"></el-table-column>
                <el-table-column prop="scheduling" label="课程学时" align="center" width="65"></el-table-column>
                <el-table-column prop="courseDate" label="课程日期" align="center"></el-table-column>
                <el-table-column prop="pop" label="热门" align="center" width="45"></el-table-column>
				<el-table-column label="状态" align="center" width="60">
                    <template #default="scope">
                        <el-switch
                                v-model="scope.row.status"
                                :active-value="'启用'"
                                :inactive-value="'不启用'"
                                active-color="#64bd63"
                                inactive-color="#c0c0c0"
                                
                                @change="changeSwitch(scope.row.status)"/>
					</template>
                </el-table-column>
				<el-table-column label="操作" width="220" align="center" >
					<template #default="scope">
						<el-row class="right-btn">
						    <el-tooltip content="修改" placement="bottom-start" effect="light">
						    <el-button color="#0ab394" :icon="Edit" @click="open(scope.$index,scope.row)"></el-button>
				        </el-tooltip>
						<el-tooltip content="删除" placement="bottom-start" effect="light">  
							<el-button color="#e35148" :icon="Delete"  @click="handleDelete(scope.$index)"></el-button>
						</el-tooltip>
							<el-tooltip content="课程设置" placement="bottom-start" effect="light">   
							<el-button color="#FBAC59"   @click="settingCourse(scope.row)" >
                                <i class="iconfont icon-wrench-fill white"></i></el-button>
						</el-tooltip>
						</el-row>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页 -->
			<div class="pagination">
				<el-pagination
				:current-page="query.pageIndex"
					:page-size="query.pageSize"
					:page-sizes="[5, 10, 15, 20]"
					size="small"
					layout="total, sizes, prev, pager, next, jumper"
					:total="pageTotal"
					@size-change="handleSizeChange"
					@current-change="handlePageChange"/>
			</div>
	</div>

		<!-- 修改/添加弹出框 -->
		<el-dialog :title="titleEdit" v-model="editVisible" width="680px"  class="dialog-add">
			<el-form label-width="75px">
				<el-form-item label="课程名称:" disabled >
					<el-input v-model="form.courseName"  :disabled="isDisable" class="input-dialog"></el-input>
				</el-form-item>
				<el-form-item label="开始时间:">
                    <el-date-picker
                        v-model="form.startTime"
                        type="date"
                        placeholder=""
                        class="input-dialog"
                    />
				</el-form-item>                
				<el-form-item label="结束时间:">
                    <el-date-picker
                        v-model="form.endTime"
                        type="date"
                        placeholder=""
						class="input-dialog"
                    />
				</el-form-item>                
				<el-form-item label="课程学时:">
					<el-input v-model="form.scheduling" class="input-dialog"></el-input>
				</el-form-item>                
				<el-form-item label="课程分类:" >
					<el-select  v-model="form.typeName"  :suffix-icon="CaretBottom" 
                    style="width: 270px;"
                    placeholder=""
                    >
                      <el-option label="Java核心技术2(测试)" value="Java核心技术2(测试)"></el-option>
                      <el-option label="服务器环境搭建" value="服务器环境搭建"></el-option>
                    </el-select>
					<el-select   :suffix-icon="CaretBottom" 
                    style="width: 270px;"
                    placeholder="">
                      <el-option label="Java核心技术2(测试)" value="Java核心技术2(测试)"></el-option>
                      <el-option label="服务器环境搭建" value="服务器环境搭建"></el-option>
                    </el-select>
				</el-form-item>                
                <el-form-item label="是否启用:">
					<el-switch
                                v-model="form.status"
                                :active-value="'启用'"
                                :inactive-value="'不启用'"
                                active-color="#64bd63"
                                inactive-color="#c0c0c0" 
								class="input-dialog"/>	
				</el-form-item>

                <el-form-item label="热门课程:">
					<!-- <el-input v-model="form.pop"  type="textarea" ></el-input> -->
                    <el-select  v-model="form.pop"  :suffix-icon="CaretBottom" 
                    class="input-dialog"
                    placeholder=""
                    >
                      <el-option label="是" value="是"></el-option>
                      <el-option label="否" value="否"></el-option>
                    </el-select>
				</el-form-item>
                <el-form-item label="公开状态:">
					<!-- <el-input v-model="form.openStatus"  type="textarea" ></el-input> -->
                    <el-select  v-model="form.openStatus"  :suffix-icon="CaretBottom" 
                    class="input-dialog"
                    placeholder="">
                      <el-option label="私有(收费)" value="私有(收费)"></el-option>
                      <el-option label="公开(免费)" value="公开(免费)"></el-option>
                    </el-select>
				</el-form-item>
                <!-- <el-form-item label="监听进度:">
					<el-select  v-model="form.monitorProgress"  :suffix-icon="CaretBottom" 
					class="input-dialog"
                    placeholder="">
                      <el-option label="不监听" value="不监听"></el-option>
                      <el-option label="监听进度" value="监听进度"></el-option>
                    </el-select>
              
				</el-form-item> -->
                <el-form-item label="课程封面:">
				<!-- <div> -->
					<vue-cropper
						class="cropper-img"
						ref="cropper"
						:src="imgSrc"
						:ready="cropImage"
						:zoom="cropImage"
						:cropmove="cropImage"
						style="width: 200px; height: 150px"
			       ></vue-cropper>
			     <el-button class="crop-demo-btn" type="success">选择图片
						<input class="crop-input" type="file" name="image" accept="image/*" @change="setImage" />
					</el-button>
					<el-button type="primary" @click="saveAvatar">剪裁</el-button>
				<!-- </div> -->
				</el-form-item>

                <el-form-item label="课程说明:">
					<el-input v-model="form.courseDesc" type="textarea" class="input-dialog"></el-input>  
				</el-form-item>
              
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button  @click="editVisible = false">关闭</el-button>
					<el-button color="#0ab394" @click="saveEdit">确 定</el-button>
				</span>
			</template>
		</el-dialog>

    

		<!-- </div> -->
	</div>
</template>

<script setup >
import { ref, reactive } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Delete, Edit, Search, Setting,CaretBottom} from '@element-plus/icons-vue';
import { getCourses } from '../../api/training' ;
import VueCropper from 'vue-cropperjs';
import 'cropperjs/dist/cropper.css';
import back from '@/assets/img/login-background.jpg';
//图片
const imgSrc = ref('')
const imgBack = ref(back)
const cropImg = ref('')
const cropper = ref()
imgSrc.value = back
const setImage = (e) => {
	console.log('eeeeee',e)
	const file = e.target.files[0];
	if (!file.type.includes('image/')) {
		return;
	}
	const reader = new FileReader();
	reader.onload = (event) => {
		imgSrc.value = event.target.result;
		cropper.value && cropper.value.replace(event.target.result);
	};
	reader.readAsDataURL(file);
};

const cropImage = () => {
	cropImg.value = cropper.value.getCroppedCanvas().toDataURL();
};
const saveAvatar = () => {
	imgBack.value = cropImg.value;
}


const  courseName= ref()
//分页字体
const small = ref(true)
// const size = ref<'default' | 'large' | 'small'>('default')

//分页数据
const query = reactive({
	pageIndex: 1,
	pageSize: 10,
});
//

//表格数据
const tableData = ref();
//总页数
const pageTotal = ref(0);
const multipleTableRef = ref()
const state = reactive({
	multipleSelection:[]
})
// 获取表格数据  分页是数据截取  后台结合一起后  直接返回分页的后的数据
const getData = () => {
    getCourses({pageCurrent:query.pageIndex,pageSize:query.pageSize}).then(res=>{
        console.log(res,"---res---")
        // tableData.value = res.data.
		let start  = (query.pageIndex-1)*query.pageSize;//开始位置
        let end =query.pageIndex*query.pageSize  ;//结束位置 数组的截取的时候 [start,end) suo所以不用-1
        tableData.value = res.data.slice(start,end);
        // tableData.value = res.data;
        pageTotal.value = res.data.length
    })
};
getData();

//多选框权限
const handleSelectionChange = (val) => {
  state.multipleSelection = val
}
// 分页导航
const handleSizeChange = (val) => {
  // console.log(`${val} items per page`)
  query.pageSize = val;
  getData()
}
const handlePageChange = (val) => {
	query.pageIndex = val;
	console.log(val,"==")
	console.log(query.pageSize)
	getData()
};

//是够启用调用后台接口
const  changeSwitch=(status)=>{
    //1 调用后台修改接口
    console.log(status,'>>>status');
	//2 重新查询数据
}

// 删除单个操作
const handleDelete = (index) => {
	// 二次确认删除
	ElMessageBox.confirm('确定要删除吗？', '提示', {
		type: 'warning'
	})
	.then(() => {
		ElMessage.success('删除成功');
		//to 传递id 到后台 
		tableData.value.splice(index, 1);
		// 删除成功后
		// getData()
	})
	.catch(() => {});
};

//删除多个数据  to......
const handleAllDelete =()=>{
	// console.log('删除多个')
	console.log(state.multipleSelection,'===拿到所有选择的数据====')
	if (state.multipleSelection.length==0) {
		ElMessage.error('请选择项')
		return
  	}
	// console.log(state.multipleSelection.map(item=>item.id))
	//axios删除的操作
	// deleteAllAdmin({ids:state.multipleSelection.map(item=>item.id)})

	
}



// 查询操作  to... 需要自己实现与后台交互
const handleSearch = () => {
	query.pageIndex = 1;
	getData();
};

// 表格编辑时弹窗和保存
const editVisible = ref(false);
let form = reactive({
    id:'',
	courseName: '',
    startTime:'',
    endTime:'',
    scheduling:'',
    typeName:'',
    status:'',
    pop:'',
    openStatus:'',
    monitorProgress:'',
    courseDesc:'',
});

// 
let titleEdit = ref('修改')
let isDisable = ref(true)
const open  = (index,row)=>{//index是时间源对象
	if(row){
        isDisable.value=true
		titleEdit.value = "修改"
		handleEdit(index,row)
	}else{
		//把第一个输出改为可编辑
		isDisable.value=false
		//清空表单中的数据
		titleEdit.value = "添加";
        form.id=''
        form.courseName=''
        form.startTime=''
        form.endTime=''
        form.scheduling=''
        form.typeName=''
        form.status=''
        form.pop=''
        form.openStatus=''
        form.monitorProgress=''
        form.courseDesc=''
		editVisible.value = true	
	}
	console.log('-------测试传值-------')
	// console.log(index,row,index.target.innerText)
	// console.log(index,row)
	// console.log('================')

}
let idx = -1;
const handleEdit = (index,row) => {
	idx = index;
    form.id = row.id;
    form.courseName=row.courseName
    form.startTime=row.start
    form.endTime=row.endTime
    form.scheduling=row.scheduling
    form.typeName=row.typeName
    form.status=row.status
    form.pop=row.pop
    form.openStatus=row.openStatus
    form.monitorProgress=row.monitorProgress
    form.courseDesc=row.courseDesc
	editVisible.value = true;
};
//保存添加或者修改的数据 ---需要重新实现
const saveEdit = () => {
	editVisible.value = false;
	ElMessage.success(`修改第 ${idx + 1} 行成功`);
	tableData.value[idx].courseName = form.courseName;

};

import { useRouter } from 'vue-router';
const router = useRouter()
//课程设置
const settingCourse =(row)=>{
	router.replace("/training/courseSetting")
}

</script >
<style lang="less" scoped>
.handle-box {
	margin-bottom: 20px;  
}

.el-select {
	width: 200px;
	margin-right: 10px;  
}

.handle-input {
	width: 200px;
    margin-right: 10px;
}
.label{
   display: inline-block;
   max-width:100%;
   margin: 5px;
   font-weight: 700; 
}
.table {
	width: 100%;
	font-size: 14px;
}
.red {
	color: #F56C6C;
}
.blue {
	color: #1296e2;
	font-weight: bold;
}

.mr10 {
	margin-right: 10px;
}
.table-td-thumb {
	display: block;
	margin: auto;
	width: 40px;
	height: 40px;
}
.cell{
    display: inline-block;
    .el-button{
        margin-left: 0px;
    }
}
.el-col {
  border-radius: 4px;
  margin-left: 10px;
}

.top-btn{
	float: right;
	.el-button{
		margin-left: 5px;
	}
}
.right-btn{
 	justify-content:center;
	.el-button{
		height: 22px;
		width: 22px;
		margin-right: 15px;
		padding: 1px 5px;
	}
}
.white{
	color:#fff;
}
// .my-select .el-select-dropdown__item {
//   width: 100px;
// //   display: inline-block;
//   overflow: hidden;
//   text-overflow: ellipsis;
//   white-space: nowrap;
// }

.crop-demo-btn {
	position: relative;
	margin-left: 10px;
}
.crop-input {
	position: absolute;
	width: 100px;
	height: 40px;
	left: 0;
	top: 0;
	opacity: 0;
	cursor: pointer;
}
.cropper-img{
	margin-left: 50px;
}
.input-dialog{
	width: 550px;
}

</style>